const encryptionStr = function(str:string):string{
    return window.encodeURI(str)
},
decryptStr = function(str:string):string{
    return window.decodeURI(str)
};
const SESSION = window.sessionStorage;

const sessionSave = (name:string,value:any,time?:number)=>{
    var data = SESSION.getItem('APP_ALL_DATA');
    var nowTime = Date.now();
    var newData = data?JSON.parse(decryptStr(data)):{};
    newData[name] = {data:value,time:time?(nowTime+1000*time):0};
    newData = JSON.stringify(newData);
    SESSION.setItem('APP_ALL_DATA',encryptionStr(newData))
}
const sessionEcho = (name:string)=>{
    var dataStr = SESSION.getItem('APP_ALL_DATA');
    var nowTime = Date.now();
    if(dataStr){
        var newData = JSON.parse(decryptStr(dataStr));
        if(!newData[name]){
            return null;
        }
        var {data,time} = newData[name];
        if(time && nowTime - time > 0){
            return null;
        }
        return data;
    }else{
        return null;
    }  
}
export function $session (name:string,value?:any,time?:number){
    if(value !== undefined){
        sessionSave(name,value,time)
    }else{
        return sessionEcho(name)
    }
}